package org.dandelion.memo.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import org.dandelion.memo.entity.MemoCategory;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MemoCategoryDao {
    private static final String TAG = "MemoCategoryDao";
    private static MemoCategoryDao memoCategoryDao;
    private static final String[] defaultCategories = {
            "未分类",
            "个人",
            "生活",
            "工作",
            "旅游",
            "学习",
    };

    public static MemoCategoryDao getInstance() {
        if (memoCategoryDao == null) {
            memoCategoryDao = new MemoCategoryDao();
        }

        return memoCategoryDao;
    }

    private MemoCategoryDao() {

    }

    public void createTable(SQLiteDatabase db) {
        String sql = "create table memo_category(" +
                "id integer primary key autoincrement," +
                "name text" +
                ");";

        Log.d(TAG, sql);
        db.execSQL(sql);


        for (String category : defaultCategories) {
            sql = "insert into memo_category " +
                    "(name) values (" +
                    "'" + category + "'" +
                    ");";

            Log.d(TAG, sql);
            db.execSQL(sql);
        }

        Log.d("MemoCategoryDao", "create table memo_category");
    }

    public void dropTable(SQLiteDatabase db) {
        db.execSQL("drop table memo_category;");
        Log.d("MemoCategoryDao", "drop table memo_category");
    }

    public void save(MemoCategory memoCategory, SQLiteDatabase db) {
        String sql = String.format(new Locale("zh-CN"),
                "insert into memo_category (name) " +
                        "values('%s');",
                memoCategory.getName()
        );

        db.execSQL(sql);
    }

    public List<MemoCategory> findAll(SQLiteDatabase db) {
        Cursor cursor = db.rawQuery("select * from memo_category", null);
        List<MemoCategory> memoCategories = new ArrayList<>();

        if (cursor.moveToFirst()) {
            do {
                MemoCategory memoCategory = new MemoCategory();
                memoCategory.setId(cursor.getInt(cursor.getColumnIndex("id")));
                memoCategory.setName(cursor.getString(cursor.getColumnIndex("name")));
            } while (cursor.moveToNext());
        }

        cursor.close();

        return memoCategories;
    }

    public MemoCategory findById(int id, SQLiteDatabase db) {
        Cursor cursor = db.rawQuery("select * from memo_category " +
                "where id='" + id + "';", null);

        MemoCategory memoCategory = null;

        if (cursor.moveToFirst()) {
            memoCategory = new MemoCategory();
            memoCategory.setId(cursor.getInt(cursor.getColumnIndex("id")));
            memoCategory.setName(cursor.getString(cursor.getColumnIndex("name")));
        }

        cursor.close();

        return memoCategory;
    }

    public void deleteById(int id, SQLiteDatabase db) {
        if (id <= 1) return;

        db.execSQL("update memo " +
                "set category_id=1 " +
                "where category_id=" + id + ";"
        );

        db.execSQL("delete from memo_category " +
                "where id='" + id + "';", null);
    }
}
